package com.service.impl;

import com.dao.AdminReaderDao;
import com.dao.ReaderDao;
import com.entity.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.AdminReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
//1
@Service
public class AdminReaderServiceImpl implements AdminReaderService {

    @Autowired
    AdminReaderDao adminReaderDao;

    //添加用户
    @Override
    public void addUser(User user) {
        adminReaderDao.addUser(user);
    }

    //修改用户信息
    @Override
    public void doUpdate(User user) {

        System.out.println(user);
        adminReaderDao.updateUser(user);
    }

    //得到所用用户
    @Override
    public PageResult getAllReaders(QueryReaderPageBean queryReaderPageBean) {
        //得到请求参数
        Integer currentPage= queryReaderPageBean.getCurrentPage();
        Integer pageSize=queryReaderPageBean.getPageSize();

        QueryReaderPageBean.ReaderQueryString readerQueryString= queryReaderPageBean.getReaderQueryString();
        String uNo= readerQueryString.getuNo();
        String uName= readerQueryString.getuName();
        String rTypeName= readerQueryString.getrTypeName();
        String unit= readerQueryString.getUnit();

        Map<String,Object> map=new HashMap();
        map.put("uNo",uNo);
        map.put("uName",uName);
        map.put("rTypeName",rTypeName);
        map.put("unit",unit);


        PageHelper.startPage(currentPage,pageSize);
        //得到所有读者
        Page<Reader> page= adminReaderDao.queryAllReaders(map);

        List<Reader> row=page.getResult();
        long total=page.getTotal();
        for(Reader reader:row){
            //计算代还数
            List<BorrowBooks> borrowBooks=adminReaderDao.queryBorrowBookByUNo(reader.getuNo());
            reader.setBorrowNum(borrowBooks.size());
//            System.out.println(borrowBooks);

        }

        PageResult pageResult=new PageResult(total,row);

        return pageResult;
    }

    //查看操作
    @Override
    public Result getBorrowBooks(Integer uNo) {
        //查询所有未还书籍
        List<BorrowBooks> borrowBooks=adminReaderDao.queryBorrowBookByUNo(uNo);
//        System.out.println(borrowBooks);

        //计算未还书籍的剩余时间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        for(BorrowBooks borrowBook:borrowBooks){
            //得到预期还书日期
            String exceptionReturn=borrowBook.getExpectReturn();

            try {
                calendar.setTime(sdf.parse(exceptionReturn));
                int exceptionReturnDays=calendar.get(Calendar.DAY_OF_YEAR);

                calendar.setTime(new Date());
                int nowDays=calendar.get(Calendar.DAY_OF_YEAR);

                //计算剩余时间
                int remindTime=exceptionReturnDays-nowDays;
                //设置剩余时间
                borrowBook.setRemindTime(remindTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        Result result=new Result(true,"ok",borrowBooks);
        return result;
    }

    //得到用户个人信息
    @Override
    public Result getReaderDetail(Integer uNo) {

        //得到读者
        Reader reader=adminReaderDao.queryReaderByUNo(uNo);

        //计算代还数
        List<BorrowBooks> borrowBooks=adminReaderDao.queryBorrowBookByUNo(reader.getuNo());
        reader.setBorrowNum(borrowBooks.size());

//        System.out.println(reader);

        Result result=new Result(true,"ok",reader);

        return result;
    }

    //挂失
    @Override
    public String doLose(Integer uNo,String uPassWord) {

        //验证用户是否存在且不是管理员且密码正确
        User user=adminReaderDao.queryUserByUNo(uNo,uPassWord);
        if(user!=null){
            //用户存在且合法
            Date nowTime=new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String loseDate=sdf.format(nowTime);
            adminReaderDao.setLose(uNo,loseDate);
            return "挂失成功";
        }else{
            //用户不存在
            throw new RuntimeException();
        }
//        System.out.println(user);
    }




}
